﻿using Core.DataAccess.Model.iTool.Gateway;
using iTool.ClusterComponent;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace HandlerContextOptions
{
    [JsonObject(MemberSerialization.OptOut)]
    public class iToolCenterOptionRequset
    {
        private int _Port { get; set; } = 2990;

        [Required]
        [Range(2990, 3997)]
        public int Port { get { return _Port; } set { _Port = value; } }

        [Required]
        public int ServicePort { get { return _Port + 1; } set { } }

        [Required]
        public int ServiceGatewayPort { get { return _Port + 2; } set { } }

        /// <summary>
        /// Accept Queue Count
        /// </summary>
        [Required]
        [Range(10, 1000)]
        public int SoBacklog { get; set; } = 100;

        /// <summary>
        /// 心跳检测时常
        /// </summary>
        [Required]
        [Range(5, 100)]
        public int HeartbeatTimeOutSeconds { get; set; } = 10;

        [Required]
        public IEnumerable<string> BaseSQLServer { get; set; } = Array.Empty<string>();
        [Required]
        public IEnumerable<string> FnsSQLServer { get; set; } = Array.Empty<string>();
        [Required]
        public IEnumerable<string> MsgSQLServer { get; set; } = Array.Empty<string>();
        [Required]
        public IEnumerable<string> LogSQLServer { get; set; } = Array.Empty<string>();
        [Required]
        public IEnumerable<string> ElasticSearch { get; set; } = new List<string>();
        public IEnumerable<string> MongoDB { get; set; } = new List<string>();


        public iToolCenterOptionRequset()
        {
            using (iToolGatewayContext context = new iToolGatewayContext())
            {
                var services = context.ServiceInstances.Where(item => item.Status == 2 & item.IsRunInDocker).Select(item => new
                {
                    item.ServiceInstanceId,
                    item.NodeId,
                    item.Title,
                    item.NodeIP,
                    item.Type,
                    item.Port,
                    item.User,
                    item.Process,
                    item.Password
                }).ToList();

                foreach (var item in services)
                {
                    switch (item.Process)
                    {
                        case "SqlServer":
                            if (!string.IsNullOrWhiteSpace(item.Type))
                            {
                                var array = item.Type.Split(',');

                                // $"Data Source={item.NodeIP},{item.Port};database=iTool.CloudBase;uid=sa;pwd={item.Password};MultipleActiveResultSets=true;"

                                foreach (var dbName in array)
                                {
                                    switch (dbName)
                                    {
                                        case "iToolBase":
                                            this.BaseSQLServerSelects.Add(new SelectOption
                                            {
                                                IP = item.NodeIP,
                                                Label = dbName, //item.Title,
                                                Value = item.ServiceInstanceId.ToString()
                                            });
                                            break;
                                        case "Logger":
                                            this.LogSQLServerSelects.Add(new SelectOption
                                            {
                                                IP = item.NodeIP,
                                                Label = dbName,//item.Title,
                                                Value = item.ServiceInstanceId.ToString()
                                            });
                                            break;
                                        case "Function":
                                            this.FnsSQLServerSelects.Add(new SelectOption
                                            {
                                                IP = item.NodeIP,
                                                Label = dbName,//item.Title,
                                                Value = item.ServiceInstanceId.ToString()
                                            });
                                            break;
                                        case "Message":
                                            this.MsgSQLServerSelects.Add(new SelectOption
                                            {
                                                IP = item.NodeIP,
                                                Label = dbName,//item.Title,
                                                Value = item.ServiceInstanceId.ToString()
                                            });
                                            break;
                                    }
                                }
                            }
                            break;
                        case "MongoDB":
                            this.MongoDBSelects.Add(new SelectOption
                            {
                                IP = item.NodeIP,
                                Label = item.Title,
                                Value = item.ServiceInstanceId.ToString()
                            });
                            break;
                        case "Elasticsearch":
                            this.ElasticsearchSelects.Add(new SelectOption
                            {
                                IP = item.NodeIP,
                                Label = item.Title,
                                Value = item.ServiceInstanceId.ToString()
                            });
                            break;
                    }
                }

            }
        }


        [JsonIgnore()]
        public List<SelectOption> BaseSQLServerSelects { get; set; } = new List<SelectOption>();
        [JsonIgnore()]
        public List<SelectOption> FnsSQLServerSelects { get; set; } = new List<SelectOption>();
        [JsonIgnore()]
        public List<SelectOption> MsgSQLServerSelects { get; set; } = new List<SelectOption>();
        [JsonIgnore()]
        public List<SelectOption> LogSQLServerSelects { get; set; } = new List<SelectOption>();
        [JsonIgnore()]
        public List<SelectOption> MongoDBSelects { get; set; } = new List<SelectOption>();
        [JsonIgnore()]
        public List<SelectOption> ElasticsearchSelects { get; set; } = new List<SelectOption>();
    }


    public class iToolCenterOptionResult : ResultBasics
    {
    }


    public class iToolConfigure
    {
        public int Port { get; set; }
        public int AcceptQueueCount { get; set; }
        public int HeartbeatTimes { get; set; }
        public int ServerPort { get; set; }
        public int ServerGatewayPort { get; set; }

        public IEnumerable<AdoNetOptions> iToolBaseDatabaseList { get; set; }
        public IEnumerable<string> iToolBaseDatabases { get; set; }
        public IEnumerable<string> FunctionDatabases { get; set; }
        public IEnumerable<string> MessageDatabases { get; set; }
        public IEnumerable<string> LoggerDatabases { get; set; }
        public IEnumerable<string> MongoDBProviders { get; set; }
        public IEnumerable<ElasticSearchProvider> ElasticSearchProviders { get; set; }
    }

    public class SelectOption
    {
        public string IP { get; set; }
        public string Label { get; set; }
        public string Value { get; set; }
    }

    public class ElasticSearchProvider
    {
        public string Ip { get; set; }
        public int Port { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
    }
}
